package com.onpositive.realmview.queryeditor.actions;

import org.eclipse.jface.action.Action;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;

import com.onpositive.realmview.IRealmView;

public final class DeleteAction extends Action {
	private final IStructuredSelection s;
	{
		setText("Delete selected element");
		setImageDescriptor(PlatformUI.getWorkbench()
				.getSharedImages().getImageDescriptor(
						ISharedImages.IMG_ETOOL_DELETE));
		setDisabledImageDescriptor(PlatformUI.getWorkbench()
				.getSharedImages().getImageDescriptor(
						ISharedImages.IMG_ETOOL_DELETE_DISABLED));
		
	}

	public DeleteAction(IStructuredSelection s) {
		this.s = s;
	}

	public void run() {
		String title = "1 element";
		if (s.size() > 1)
			title = s.size() + " elements";
		if (!MessageDialog.openQuestion(Display.getDefault().getActiveShell(),"Delete","Delete " + title + ".Are you sure?"))
			return;
		Object[] array = s.toArray();
		for (Object o : array) {
			if (o instanceof IRealmView){
				IRealmView v=(IRealmView) o;
				v.delete();	
				continue;
			}
		}
	}
}